<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="公司代码" prop="gcdm">
                    <el-input
                        v-model="queryParams.gcdm"
                        placeholder="请输入公司代码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="维保内容" prop="wbnr">
                    <el-input
                        v-model="queryParams.wbnr"
                        placeholder="请输入维保内容"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="维保单价" prop="wbdj">
                    <el-input
                        v-model="queryParams.wbdj"
                        placeholder="请输入维保单价"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="维保人工号" prop="wbrgh">
                    <el-input
                        v-model="queryParams.wbrgh"
                        placeholder="请输入维保人工号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="维保人姓名" prop="wbrxm">
                    <el-input
                        v-model="queryParams.wbrxm"
                        placeholder="请输入维保人姓名"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="管理人工号" prop="glrgh">
                    <el-input
                        v-model="queryParams.glrgh"
                        placeholder="请输入管理人工号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="管理人姓名" prop="glrxm">
                    <el-input
                        v-model="queryParams.glrxm"
                        placeholder="请输入管理人姓名"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="备注" prop="bz">
                    <el-input
                        v-model="queryParams.bz"
                        placeholder="请输入备注"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="公司名称" prop="gcmc">
                    <el-input
                        v-model="queryParams.gcmc"
                        placeholder="请输入公司名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="设备编码" prop="sbbm">
                    <el-input
                        v-model="queryParams.sbbm"
                        placeholder="请输入设备编码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="设备名称" prop="sbmc">
                    <el-input
                        v-model="queryParams.sbmc"
                        placeholder="请输入设备名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="购买日期" prop="gmrq">
                    <el-date-picker clearable
                                    v-model="queryParams.gmrq"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择购买日期">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="安装日期" prop="azrq">
                    <el-date-picker clearable
                                    v-model="queryParams.azrq"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择安装日期">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="维保周期" prop="wbzq">
                    <el-input
                        v-model="queryParams.wbzq"
                        placeholder="请输入维保周期"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="计划维保维保日期" prop="jhwbwbrq">
                    <el-date-picker clearable
                                    v-model="queryParams.jhwbwbrq"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择计划维保维保日期">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="维保内容编码" prop="wbnrbm">
                    <el-input
                        v-model="queryParams.wbnrbm"
                        placeholder="请输入维保内容编码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['device:wbgz:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['device:wbgz:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['device:wbgz:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['device:wbgz:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wbgzList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="主键" align="center" prop="id" />
              <el-table-column label="公司代码" align="center" prop="gcdm" />
              <el-table-column label="维保内容" align="center" prop="wbnr" />
              <el-table-column label="维保单价" align="center" prop="wbdj" />
              <el-table-column label="维保人工号" align="center" prop="wbrgh" />
              <el-table-column label="维保人姓名" align="center" prop="wbrxm" />
              <el-table-column label="管理人工号" align="center" prop="glrgh" />
              <el-table-column label="管理人姓名" align="center" prop="glrxm" />
              <el-table-column label="备注" align="center" prop="bz" />
              <el-table-column label="公司名称" align="center" prop="gcmc" />
              <el-table-column label="设备编码" align="center" prop="sbbm" />
              <el-table-column label="设备名称" align="center" prop="sbmc" />
              <el-table-column label="购买日期" align="center" prop="gmrq" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.gmrq, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="安装日期" align="center" prop="azrq" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.azrq, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="维保周期" align="center" prop="wbzq" />
              <el-table-column label="计划维保维保日期" align="center" prop="jhwbwbrq" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.jhwbwbrq, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="维保内容编码" align="center" prop="wbnrbm" />
              <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['device:wbgz:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['device:wbgz:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改设备维保规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                        <el-form-item label="公司代码" prop="gcdm">
                          <el-input v-model="form.gcdm" placeholder="请输入公司代码" />
                        </el-form-item>
                        <el-form-item label="维保内容" prop="wbnr">
                          <el-input v-model="form.wbnr" placeholder="请输入维保内容" />
                        </el-form-item>
                        <el-form-item label="维保单价" prop="wbdj">
                          <el-input v-model="form.wbdj" placeholder="请输入维保单价" />
                        </el-form-item>
                        <el-form-item label="维保人工号" prop="wbrgh">
                          <el-input v-model="form.wbrgh" placeholder="请输入维保人工号" />
                        </el-form-item>
                        <el-form-item label="维保人姓名" prop="wbrxm">
                          <el-input v-model="form.wbrxm" placeholder="请输入维保人姓名" />
                        </el-form-item>
                        <el-form-item label="管理人工号" prop="glrgh">
                          <el-input v-model="form.glrgh" placeholder="请输入管理人工号" />
                        </el-form-item>
                        <el-form-item label="管理人姓名" prop="glrxm">
                          <el-input v-model="form.glrxm" placeholder="请输入管理人姓名" />
                        </el-form-item>
                        <el-form-item label="备注" prop="bz">
                          <el-input v-model="form.bz" placeholder="请输入备注" />
                        </el-form-item>
                        <el-form-item label="公司名称" prop="gcmc">
                          <el-input v-model="form.gcmc" placeholder="请输入公司名称" />
                        </el-form-item>
                        <el-form-item label="设备编码" prop="sbbm">
                          <el-input v-model="form.sbbm" placeholder="请输入设备编码" />
                        </el-form-item>
                        <el-form-item label="设备名称" prop="sbmc">
                          <el-input v-model="form.sbmc" placeholder="请输入设备名称" />
                        </el-form-item>
                        <el-form-item label="购买日期" prop="gmrq">
                          <el-date-picker clearable
                                          v-model="form.gmrq"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择购买日期">
                          </el-date-picker>
                        </el-form-item>
                        <el-form-item label="安装日期" prop="azrq">
                          <el-date-picker clearable
                                          v-model="form.azrq"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择安装日期">
                          </el-date-picker>
                        </el-form-item>
                        <el-form-item label="维保周期" prop="wbzq">
                          <el-input v-model="form.wbzq" placeholder="请输入维保周期" />
                        </el-form-item>
                        <el-form-item label="计划维保维保日期" prop="jhwbwbrq">
                          <el-date-picker clearable
                                          v-model="form.jhwbwbrq"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择计划维保维保日期">
                          </el-date-picker>
                        </el-form-item>
                        <el-form-item label="维保内容编码" prop="wbnrbm">
                          <el-input v-model="form.wbnrbm" placeholder="请输入维保内容编码" />
                        </el-form-item>
                        <el-form-item label="备注" prop="remark">
                          <el-input v-model="form.remark" placeholder="请输入备注" />
                        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listWbgz, getWbgz, delWbgz, addWbgz, updateWbgz } from "@/api/device/wbgz";
  import {getToken} from "@/utils/auth";
  export default {
    name: "Wbgz",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备维保规则表格数据
              wbgzList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                        gcdm: null,
                        wbnr: null,
                        wbdj: null,
                        wbrgh: null,
                        wbrxm: null,
                        glrgh: null,
                        glrxm: null,
                        bz: null,
                        gcmc: null,
                        sbbm: null,
                        sbmc: null,
                        gmrq: null,
                        azrq: null,
                        wbzq: null,
                        jhwbwbrq: null,
                        wbnrbm: null,
        },
        upload: {
          // 是否显示弹出层（物料导入）
          open: false,
          // 弹出层标题（物料导入）
          title: "导入`wbgz`信息",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/device/wbgz/importData"
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询设备维保规则列表 */
      getList() {
        this.loading = true;
        listWbgz(this.queryParams).then(response => {
          this.wbgzList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        id: null,
                        xh: null,
                        gcdm: null,
                        wbnr: null,
                        wbdj: null,
                        wbrgh: null,
                        wbrxm: null,
                        glrgh: null,
                        glrxm: null,
                        bz: null,
                        gcmc: null,
                        sbbm: null,
                        sbmc: null,
                        gmrq: null,
                        azrq: null,
                        wbzq: null,
                        jhwbwbrq: null,
                        wbnrbm: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null,
                        remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加设备维保规则";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWbgz(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改设备维保规则";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWbgz(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWbgz(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除设备维保规则编号为"' + ids + '"的数据项？').then(function() {
          return delWbgz(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('device/wbgz/export', {
      ...this.queryParams
    }, `wbgz_${new Date().getTime()}.xlsx`)
  },
  /** 导入按钮操作 */
  handleImport() {
    this.upload.title = "`设备维保规则导入";
    this.upload.open = true;
  },
  /** 下载模板操作 */
  importTemplate() {
    this.download('device/wbgz/importTemplate', {
    }, `设备维保规则模板.xlsx`)
  },
  // 文件上传中处理
  handleFileUploadProgress(event, file, fileList) {
    this.upload.isUploading = true;
  },
  // 文件上传成功处理
  handleFileSuccess(response, file, fileList) {
    this.upload.open = false;
    this.upload.isUploading = false;
    this.$refs.upload.clearFiles();
    this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
    this.getList();
  },
  // 提交上传文件
  submitFileForm() {
    this.$refs.upload.submit();
  },
  }
  };
</script>
